package cn.ppool.xmlmapper.utils;


import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class VmfResource {


    private static InputStream preserveSubpackageName(final Resource resource) {
        try {
            return resource.getInputStream();
        } catch (IOException e) {
            return null;
        }
    }

    public static List<InputStream> findResourcesAsInputStream(String path, ClassLoader classLoader) throws Exception {
        ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver(classLoader);
        Resource[] resources = resourceResolver.getResources(path);
        return Stream.of(resources).map(VmfResource::preserveSubpackageName).collect(Collectors.toList());
    }

    public static List<InputStream> findResourcesAsInputStream(String path, ResourceLoader resourceLoader) throws Exception {
        ResourcePatternResolver resourceResolver = (ResourcePatternResolver) resourceLoader;
        Resource[] resources = resourceResolver.getResources(path);
        return Stream.of(resources).map(VmfResource::preserveSubpackageName).collect(Collectors.toList());
    }



}
